﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TLTuan.App.AccessLayer;
using TLTuan.App.TransferObj;
using System.Data;

namespace TLTuan.Admin
{
    public partial class Login_Admin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtUser.Focus();
            lbl_error.Visible = false;
            if (!IsPostBack)
            {
                System.Data.DataTable table = (System.Data.DataTable)Session["ThanhVien"];
                if (table != null && table.Rows.Count>0)
                    Response.Redirect("default.aspx");
            }
        }

        protected void cmd_reset_Click(object sender, EventArgs e)
        {

        }

        protected void cmd_login_Click(object sender, EventArgs e)
        {
            CDAOThanhVien objUserDAO = new CDAOThanhVien();
            string v_str_pass = txtPass.Text;

            DataTable v_dt =  objUserDAO.LoginSuccess(txtUser.Text.Trim(),txtPass.Text.Trim());
            
            if (v_dt.Rows.Count > 0)
            {
                if (objUserDAO.CheckRole(int.Parse(v_dt.Rows[0]["FK_NhomID"].ToString())))
                {
                    Session["ThanhVien"] = v_dt;
                    Response.Redirect("Default.aspx");
                }
                else
                {
                    lbl_error.Visible = true;
                    lbl_error.Text = "Tài khoản đã bị khóa";
                }
            }
            else
            {
                lbl_error.Visible = true;
                lbl_error.Text = "Username hoặc password không đúng.";
            }
        }
    }
}